package com.dyylearn.servlet.annotation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 注解的方式来配置
 * 1.@WebServLet是一个注解=>java基础->注解
 * 2.@WebServlet源码
 * 3.urlPatterns对应web.xml的<url-pattern></url-pattern>
 * 4.{"/ok1"，"/ok2"}可以给0kServlet配置多个urL-pattern
 * 5.相当于这个@WebServLet（urLPatterns={"/ok1"，"/ok2"}）代替了web.xmL的配置
 * 底层使用了反射+注解+I0+集合来完成一个支撑
 * 6.浏览器可以这样访问0kServlet时，可以http：//Localhost：8080/servlet/ok1 或者
 *  http：//Localhost：8080/servlet/ok2
 * 8.我们可以根据@interfaceWebServlet源码知道可以配置哪些
 * web.xml init-param在注解中，如何指定呢？
 *         <init-param>
 *             <param-name></param-name>
 *             <param-value></param-value>
 *         </init-param>
 * 9.注解方式开发ServLet和 web.xmL配置servLet流程机制是一样
 * 10./ok1/aa/ok1/bb/ok1/cc/ok1/aa/bb/cc/ok2
 * 11.*.action 这时 zs.action ls.action
 */
//@WebServlet(urlPatterns = {"*.action", "/ok1"}, loadOnStartup = 1, initParams = {@WebInitParam(name="xx", value = "yy"), @WebInitParam(name="zz", value = "kk")})
@WebServlet(urlPatterns = {"/ok1/aa"})
public class OkServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("注解方式 OkServlet init()被调用");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注解方式 OkServlet doPost");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(10 / 0);
        System.out.println("注解方式 OkServlet doGet");
    }
}
